package com.sxtyxt.online.me;

import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;

import com.sxtyxt.online.ActBase;
import com.sxtyxt.online.R;
import com.sxtyxt.online.SystemConst;
import com.sxtyxt.util.GsonUtil;
import com.sxtyxt.util.TextUtil;
import com.sxtyxt.volley.LoadingDialogResultListenerImpl;
import com.sxtyxt.volley.URL;
import com.sxtyxt.volley.VolleyUtils;

/**
 * 注册页面
 *
 * @author General
 */
public class ActFindPwd extends ActBase implements View.OnClickListener {

    private EditText etxtPhone, etxtCode;
    private Button btnGetCode;
    private String code;
    private String memberId;
    private Chronometer timer;
    // 倒计时;
    private static final int COUNTMAX = 120;
    private int countdownNum;

    @Override
    protected int getLayoutId() {
        return R.layout.find_pwd;
    }

    @Override
    protected void initUI() {
        super.initUI();
        etxtPhone = (EditText) findViewById(R.id.etxt_phone);
        etxtCode = (EditText) findViewById(R.id.etxt_code);
        btnGetCode = (Button) findViewById(R.id.btn_get_code);
        btnGetCode.setOnClickListener(this);
        timer = (Chronometer) findViewById(R.id.chronometer);
        timer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {

            @Override
            public void onChronometerTick(Chronometer arg0) {
                if (countdownNum <= 0) {
                    resetSendBtn();
                    return;
                }
                btnGetCode.setText(String.format("%d秒",countdownNum));
                countdownNum--;
            }
        });

        mTitleBar.mSetOnRightButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtil.isMobileNumber(getStringByUI(etxtPhone))) {
                    doToast("手机号不合法");
                    return;
                }
                if (code == null || !code.equals(getStringByUI(etxtCode))) {
                    doToast("验证码错误");
                    return;
                }
                transfer(ActResetPwd.class,memberId,"memberId");
                finish();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_get_code:
                if (!TextUtil.isMobileNumber(getStringByUI(etxtPhone))) {
                    doToast("手机号不合法");
                    return;
                }
                sendCodeToPhone();
                break;
        }
    }

    /**
     * 发送验证码
     */
    private void sendCodeToPhone() {
        VolleyUtils.requestService(SystemConst.FIND_CODE, URL.getSendCode(getStringByUI(etxtPhone)),
                new LoadingDialogResultListenerImpl(this, "正在发送验证码") {
                    @Override
                    public void onSuccess(String response) {
                        super.onSuccess(response);
                        CodeVO vo = GsonUtil.deser(response, CodeVO.class);
                        if (vo == null) {
                            return;
                        }
                        if (vo.result == 1) {
                            if (vo.data != null) {
                                code = vo.data.verifyCode;
                                memberId = vo.data.memberId;
                                doCountdown();
                            } else {
                                doToast("获取验证码失败，请重试");
                            }
                        } else {
                            doToast(vo.msg);
                        }


                    }

                    @Override
                    public void onError() {
                        super.onError();
                    }
                });
    }

    class CodeVO {
        public int result;
        public String msg;
        public Code data;
    }

    class Code {
        public String verifyCode;
        public String memberId;
    }

    protected void resetSendBtn() {
        btnGetCode.setText("获取验证码");
        btnGetCode.setEnabled(true);
        timer.stop();
    }

    /**
     * 倒计时
     */
    private void doCountdown() {
        countdownNum = COUNTMAX;
        btnGetCode.setEnabled(false);
        btnGetCode.setFocusable(false);
        timer.start();
    }

}
